package com.neuedu.shop.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.neuedu.shop.pojo.User;
import com.neuedu.shop.service.UserService;

/**
 * @author ZhangHongRui
 *
 *         2019年12月27日
 */
/**
 * @author ZhangHongRui
 *
 *         2020年1月11日
 */
@Controller
public class UserController {

	@Autowired
	private UserService service;

	@RequestMapping("/back/findAll.user")
	public String findAll(ModelMap map) {
		List<User> users = service.findAll();
//		System.out.println(users);
		// 将查询到的结果绑定到map对象当中
		map.addAttribute("users", users);
		// 告诉Controller层要将结果交给哪个页面显示 转发
		return "forward:user_list.jsp";
	}

	@RequestMapping("/pre/register.user")
	public String preRegister(User user, String hcity, String hproper, String harea, String addr2) {
		if (harea == null)
			harea = "";
		user.setAddr(hcity + hproper + harea + addr2);
		service.insert(user);
		System.out.println(user);
		// 如果页面跳转，需要用重定向
		return "redirect:user_login.jsp";
	}

	@RequestMapping("/back/register.user")
	public String backRegister(User user) {
		service.insert(user);
		// 如果页面跳转，需要用重定向
		return "redirect:findAll.user";
	}

	@RequestMapping("/back/delete.user")
	public String delete(Integer id) {
		service.delete(id);
		return "redirect:findAll.user";
		// 到查询界面列表请求
	}

	@RequestMapping("/back/resetPassword.user")
	public String resetPassword(Integer id) {
		service.resetPassword(id);
		return "redirect:findAll.user";
	}

	@RequestMapping("/pre/login.user")
	public String login(String name, String pwd, String inputcode, HttpSession session, ModelMap map) {
		User user = service.login(name, pwd);
		// 用户和密码输入正确
		if (user != null) {
			String code = (String) session.getAttribute("code");
			// 表示验证码也相同
			if (code.equals(inputcode)) {
				// 将登录正确的user对象绑定到session对象中
				session.setAttribute("user", user);
				return "redirect:index.product";
			} else {
				// 给出错误提示
				map.addAttribute("code_msg", "验证码错误");
				return "forward:user_login.jsp";
			}

		} else {
			// 给出错误提示
			map.addAttribute("error_msg", "用户名或密码错误");
			return "forward:user_login.jsp";
		}

	}

	@RequestMapping("/pre/logout.user")
	public String logout(HttpSession session) {
		session.invalidate();
		return "redirect:user_login.jsp";
	}

	/**
	 * 前台修改用户信息
	 * 
	 * @author pan
	 * @param user
	 */
	@RequestMapping("/pre/update.user")
	public String update(User user, String harea, String hproper, String hcity, String addr2) {
		if (harea == null)
			harea = "";
		user.setAddr(hcity + hproper + harea + addr2);
		System.out.println("Controller");
		System.out.println(user);
		service.update(user);
		return "redirect:jump.jsp";
	}

	/**
	 * 用户列表批量删除
	 * 
	 * @author 潘启珏
	 * @param ids
	 * @return
	 */
	@RequestMapping("/back/delete.users")
	public String delete(int[] ids) {
		for (int i = 0; i < ids.length; i++) {
			System.out.println(ids[i]);
		}
		service.batchDelete(ids);
		return "redirect:findAll.user";
	}

}
